<?php
class YelpHelper extends Helper
{
	
	var $yelpID = "bb7U2kUGpJT6uKBwwHzDPg";
	
	//Refer to http://www.yelp.com/developers/documentation/search_api: Check parameters type!
	//$yelpID, $term, $lim => Optional ||| $tl_lat,$tl_long,$br_lat,$br_long => Mandatory
	function constructMapBounding($yelpID = "bb7U2kUGpJT6uKBwwHzDPg",$term=NULL,$limit=NULL,$tl_lat,$tl_long,$br_lat,$br_long)
	{
		$url = "http://api.yelp.com/business_review_search";
		if($term!=NULL)
		{
			$url = $url."?term=".rawurlencode($term);
		}
		if($limit!=NULL)
		{
			$url = $url."&limit=".$limit;
		}
		if($tl_lat!=NULL)
		{
			$url = $url."&tl_lat=".$tl_lat;
		}

		if($tl_long!=NULL)
		{
			$url = $url."&tl_long=".$tl_long;
		}

		if($br_lat!=NULL)
		{
			$url = $url."&br_lat=".$br_lat;
		}

		if($br_long!=NULL)
		{
			$url = $url."&br_long=".$br_long;
		}

		$url = $url."&ywsid=".$yelpID;
		return $url;	
	}
	//Refer to http://www.yelp.com/developers/documentation/search_api: Check parameters type!
	//$yelpID, $term, $limit, $cc, $radius => Optional ||| $location => Mandatory
	function constructAddress($yelpID = "bb7U2kUGpJT6uKBwwHzDPg",$term=NULL,$limit=NULL,$location,$cc = "US",$radius=NULL)
	{
		$url = "http://api.yelp.com/business_review_search";
		$no_Param = false;
		if($term!=NULL)
		{
			$url = $url."?term=".rawurlencode($term);
		}else{$no_Param=true;}
		if($limit!=NULL)
		{
			$url = $url."&limit=".$limit;
		}else{$no_Param=true;}
		if($location!=NULL)
		{
			$url = $url."&location=".rawurlencode($location);
		}else{$no_Param=true;}
		if($radius!=NULL)
		{
			$url = $url."&radius=".$radius;
		}else{$no_Param=true;}
		
		$url = $url."&ywsid=".$yelpID;
		
		//Check if we have been able to construct the URL (missing parameters) to connect to the API: If not, tell the user.
		if($no_Param==true)
		{
			$url = 404;
		}

		return $url;		
	}
	//Refer to http://www.yelp.com/developers/documentation/search_api: Check parameters type!
	//$yelpID, $term, $lim, $radius => Optional ||| $lat, $long => Mandatory
	function constructGeoPoint($yelpID = "bb7U2kUGpJT6uKBwwHzDPg",$term=NULL,$limit=NULL,$lat,$long,$radius)
	{
		$url = "http://api.yelp.com/business_review_search";
		$no_Param = false;

		if($term!=NULL)
		{
			$url = $url."?term=".rawurlencode($term);
		}else{$no_Param=true;}
		if($limit!=NULL)
		{
			$url = $url."&limit=".$limit;
		}else{$no_Param=true;}
		if($lat!=NULL)
		{
			$url = $url."&lat=".$lat;
		}else{$no_Param=true;}
		if($long!=NULL)
		{
			$url = $url."&long=".$long;
		}else{$no_Param=true;}
		if($radius!=NULL)
		{
			$url = $url."&radius=".$radius;
		}else{$no_Param=true;}
		
		$url = $url."&ywsid=".$yelpID;
		
		if($no_Param==true)
		{
			$url = 404;
		}
		
		return $url;	
	}

	function init($url)
	{	
		$yelpString = file_get_contents($url);
		$obj = json_decode($yelpString);// Convert JSON from yelp return string
		//Check for errors on the message:
		$message = $obj->message;
		//For location related errors, we throw specific error		
		if($message->code == 200 || $message->code == 201 || $message->code == 202)
		{
			$obj = NULL;
		}
		return $obj;
	}
	function returnAddress1($business)
	{
		return $business->address1;
	}
	function returnAddress2($business)
	{
		return $business->address2;
	}
	function returnAddress3($business)
	{
		return $business->address3;
	}
	function getRating($business)
	{
		return $business->rating_img_url;
	}
	function getReviewCount($business)
	{
		return $business->review_count;
	}
	function getUrl($business)
	{
		return $business->url;
	}
	
	function printPhoto($business)
	{
		echo "<img border=0 src='".$business->photo_url."'><br/>";
	}
	function printName($business)
	{
		return $business->name;
	}
	function getReviews($business,$n_reviews)
	{
		$num_Reviews = count($business->reviews);
		$array_Reviews = array();
		//Compare the original num reviews in the object with the n_reviews we are asking for.
		//If the original reviews is little than what we want, we throw a message to the user
		if($num_Reviews < $n_reviews)
		{
			echo "We could just provide ".$num_Reviews."</br>";
			$iterator = $num_Reviews;
		}
		else
		{
			$iterator = $n_reviews;
		}
			for($i=0;$i<$iterator;$i++)
			{
					$array_Comment = array($business->reviews[$i]->text_excerpt,$business->reviews[$i]->url);
					array_push($array_Reviews,$array_Comment);
			}
		return $array_Reviews;
	}

	function printYelpLogo()
	{
		//Visit http://www.yelp.com/developers/getting_started/display_requirements to choose your log
		$yelpLogo1 = "http://static1.px.yelpcdn.com/static/200911301285253944/i/developers/Powered_By_Yelp_Red.png";
		echo "<img border=0 src='".$yelpLogo1."'><br/>";
	}
	function returnNearest($businesses,$origLat,$origLong)
	{
		$NearestDistnce = 50;
		
		foreach($businesses as $business):
			//Get geo points to compare with the original
			
			$busLat = $business->latitude;
			$busLong = $business->longitude;
			//NEAREST NEIGHBOR
			//Diference in Latitude
			$difLat = $busLat - $origLat;

			//Power of diference in Latitude
			$powLat = pow($difLat,2);
			//Diference in Longitude
			$difLong = $busLong - $origLong;
			//Power of diference in Latitude
			$powLong = pow($difLong,2);
			//echo "PowerLong is: ".$powLong;
			//Addition of these two components
			$addGeo = $powLat + $powLong;
			//Square -> Euclidean Distance
			$EuclDistn = sqrt($addGeo);
			if($EuclDistn < $NearestDistnce)
			{
				$NearestDistnce = $EuclDistn;
				$businessOK = $business;
			}
			//echo "<img border=0 src='".$business->photo_url."'><br/>";
		endforeach;

		if($NearestDistnce > 0.001)
		{
			return NULL;
		}
		else
		{
			return $businessOK;
		}
	}
	
}

?>